package com.example.service;

import com.example.dao.BookDao;
import com.example.dao.TypeDao;
import com.example.entity.Book;
import com.example.entity.Params;
import com.example.entity.dto.InfoDTO;
import com.example.entity.dto.UserDTO;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;


@Service
public class BookService {

    @Resource
    private BookDao bookDao;
    @Resource
    private TypeDao typeDao;


    public PageInfo<Book> findBySearch(Params params) {
        // 开启分页查询
        PageHelper.startPage(params.getPageNum(), params.getPageSize());
        // 接下来的查询会自动按照当前开启的分页设置来查询
        List<Book> list = bookDao.findBySearch(params);
        return PageInfo.of(list);
    }

    public void addStudent(UserDTO userDTO) {
        InfoDTO infoDTO =  typeDao.selectInforByIdCard(userDTO);
        Book book = new Book();
        book.setCollege(infoDTO.getCollege());
        book.setMajor(infoDTO.getMajor());
        book.setNumber(infoDTO.getNumber());

        book.setName(userDTO.getName());
        book.setPhoto(userDTO.getImg());
        bookDao.addStudent(book);
    }
    public List<Book> findAll() {
        return bookDao.selectAll();
    }

    public void updateStudent(UserDTO userDTO) {
        InfoDTO infoDTO =  typeDao.selectInforByIdCard(userDTO);
        Book book = new Book();
        book.setCollege(infoDTO.getCollege());
        book.setMajor(infoDTO.getMajor());
        book.setNumber(infoDTO.getNumber());

        book.setName(userDTO.getName());
        book.setPhoto(userDTO.getImg());
        bookDao.updateStudent(book);
    }

    public int selectCount(String number) {
        return bookDao.selectCountByNumber(number);
    }

//    public void add(UserDTO book) {
//        bookDao.insertSelective(book);
//    }
//
//    public void update(Book book) {
//        bookDao.updateByPrimaryKeySelective(book);
//    }
//
//    public void delete(Integer id) {
//        bookDao.deleteByPrimaryKey(id);
//    }
}
